﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using FreedomX2.Core.Log;

namespace FreedomX2.Core.Transport.ServiceProvider
{
    public class ServiceCenter
    {
        private ServiceHost server = null;
        private BasicHttpBinding httpBinding;
        private NetTcpBinding tcpBinding;
        public ServiceCenter(string ipAddress, Type ServiceMainClass, string NameSpace)
        {
            HttpBaseAddress = new Uri("http://" + ipAddress + ":1011/" + NameSpace);
            TcpBaseAddress = new Uri("net.tcp://" + ipAddress + ":1012/" + NameSpace);
            server = new ServiceHost(ServiceMainClass, TcpBaseAddress, HttpBaseAddress);
            httpBinding = new BasicHttpBinding();
            httpBinding.MaxReceivedMessageSize = int.MaxValue;
            httpBinding.MaxBufferSize = int.MaxValue;
            httpBinding.MaxBufferPoolSize = int.MaxValue;
            httpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
            httpBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            httpBinding.Security.Mode = BasicHttpSecurityMode.None;
            tcpBinding = new NetTcpBinding();
            tcpBinding.Security.Mode = SecurityMode.None;
            tcpBinding.MaxConnections = 999999;
        }
        public Uri HttpBaseAddress { get; private set; }
        public Uri TcpBaseAddress { get; private set; }

        public void Start()
        {
            ServiceThrottlingBehavior behaviorTcp = server.Description.Behaviors.Find<ServiceThrottlingBehavior>();
            ServiceMetadataBehavior behaviorHttp = server.Description.Behaviors.Find<ServiceMetadataBehavior>();
            {
                if (behaviorHttp == null)
                {
                    behaviorHttp = new ServiceMetadataBehavior();
                    behaviorHttp.HttpGetEnabled = true;
                    server.Description.Behaviors.Add(behaviorHttp);
                }
                if (behaviorTcp == null)
                {
                    behaviorTcp = new ServiceThrottlingBehavior();
                    behaviorTcp.MaxConcurrentInstances = 200;
                    behaviorTcp.MaxConcurrentSessions = 10000;
                    behaviorTcp.MaxConcurrentInstances = 99999999;
                    server.Description.Behaviors.Add(behaviorTcp);
                }
            }

            server.Opened += delegate
            {
                Console.WriteLine("Service Begin Listening !");
            };
            server.Faulted += delegate(object sender, EventArgs e)
            {
                Console.WriteLine("Service Faulted !");
            };
            server.UnknownMessageReceived += delegate(object sender, UnknownMessageReceivedEventArgs e)
            {
                Console.WriteLine("Service UnknownMessageReceived : \n\r" + e.Message);
            };
            server.Open();
        }
        public void Close()
        {
            server.Close();
        }

        public void AddInterfaceListener(Type ServiceInterface)
        {
#if DEBUG
            Console.WriteLine("Binding " + ServiceInterface.Name + " to Current Service");
#endif
            try
            {
                server.AddServiceEndpoint(ServiceInterface, httpBinding, string.Empty);
            }
            catch (System.Exception _ex)
            {
                _ex.e(ServiceInterface.ToString() + " HttpBinding Error " + _ex.Message);
            }
            try
            {
                server.AddServiceEndpoint(ServiceInterface, tcpBinding, string.Empty);
            }
            catch (System.Exception _ex)
            {
                _ex.e(ServiceInterface.ToString() + " TcpBinding Error " + _ex.Message);
            }
        }
    }
}
